import {getUsers} from "../personal/personal";

export default {
    ws : null,
    nameMap: new Map(),
    connect(nm){
        const _this = this
        const h = this.$createElement;
        getUsers(window.sessionStorage.getItem('username')).then(res => {
            let contacts = new Map(Object.entries(res.data));
            for(let item of contacts.values()){
                for(let item1 of item){
                    _this.nameMap.set(item1.username, item1.fullName)
                }
            }
        }).then( res => {
            this.ws = new WebSocket("ws://localhost:6001/chart/" + window.sessionStorage.getItem('username'));
            this.ws.onmessage = function (evt){
                const es = JSON.parse(evt.data)
                nm.$notify({
                    title: _this.nameMap.get(es.username) + '',
                    message: nm.$createElement('i', { style: 'color: teal'}, '发来了一条消息')
                });
                let cs = window.localStorage.getItem(es.username)
                cs = JSON.parse(cs)
                if(cs == null){
                    cs = []
                }
                cs.push({username:es.username, text: es.text})
                window.localStorage.setItem(es.username, JSON.stringify(cs))
            }

        })

    }
}
